package com.wutuo.wtservice.provider.dao;

import com.wutuo.wtservice.api.request.ListUserServiceRequest;
import com.wutuo.wtservice.api.request.UserServiceRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 服务关系表 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */
public class UserServiceSqlProvider extends BaseUserServiceSqlProvider {

    public String lisForPage(ListUserServiceRequest request) {

        BEGIN();
        getSelected();
        FROM("user_service a , service b ");

        SqlBuilder.WHERE(" a.wt_service_id = b.id ");
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        SqlBuilder.WHERE(" b.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (!StringUtils.isEmpty(request.getIds())) {
            SqlBuilder.WHERE(" a.id in (" + request.getIds() + ")");
        }

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String list(UserServiceRequest request) {

        BEGIN();
        getSelected();
        FROM("user_service a , service b ");

        SqlBuilder.WHERE(" a.wt_service_id = b.id ");
        if (!StringUtils.isEmpty(request.getIds())) {
            SqlBuilder.WHERE(" a.id in (" + request.getIds() + ")");
        }

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        SqlBuilder.WHERE(" b.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {

        SELECT(
            "b.id as typeId,b.name as typeName,a.id,wt_service_id as wtServiceId,a.roster_id as rosterId,a.create_by as createBy,a.create_date as createDate,a.update_by as updateBy,a.update_date as updateDate,a.remarks as remarks,a.del_flag as delFlag");
    }

}